﻿using HrEFCore;
using HrService;
using Microsoft.AspNetCore.Mvc;

namespace HrUI.Controllers
{
    public class CMKController : Controller
    {
        private readonly MyDbContext myDbContext;
        private readonly CMKService cMKService;
        public CMKController(MyDbContext myDbContext, CMKService cMKService)
        {
            this.myDbContext = myDbContext;
            this.cMKService = cMKService;
        }
        public async Task<IActionResult> Index()
        {
            //查询所有
            List<CMK> cmkList = await cMKService.InquireAll();
            ViewData.Model = cmkList;
            return View();
        }
        public IActionResult Add()
        {
            //跳转添加页面
            return View();
        }
        public async Task<IActionResult> Add2(CMK cMK)
        {
            //添加的逻辑代码
            int zhi = await cMKService.InquireMajorKindId();
            string zhi2 = (zhi + 1).ToString();
            cMK.MajorKindId = zhi2;

            int result = await cMKService.Add(cMK);
            if (result > 0)
            {
                // 添加成功
                Content("<script>alert('添加成功！')</script>");
                return RedirectToAction("Index");
            }
            else
            {
                // 添加失败，返回添加职位分类的页面，并提示用户
                Content("<script>alert('添加失败！')</script>");
                return View();
            }
        }
        public async Task<IActionResult> Delete(string id)
        {
            short mfkId;
            if (!short.TryParse(id, out mfkId))
            {
                // ID格式不正确，返回职位分类列表，并提示用户
                Content("<script>alert('参数错误！')</script>");
                return RedirectToAction("Index");
            }
            int result = await cMKService.DeleteMfkId(mfkId);
            if (result > 0)
            {
                // 删除成功
                Content("<script>alert('删除成功！')</script>");
            }
            else
            {
                // 删除失败，返回职位分类列表，并提示用户
                Content("<script>alert('删除失败！')</script>");
            }
            return RedirectToAction("Index");
        }
    }
}
